<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kafka时间轮机制深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: shimmer 3s infinite;
        }
        
        @keyframes shimmer {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .feature-card {
            background: white;
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            border: 1px solid rgba(102, 126, 234, 0.1);
        }
        
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 40px rgba(102, 126, 234, 0.2);
            border-color: rgba(102, 126, 234, 0.3);
        }
        
        .icon-wrapper {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 20px;
        }
        
        .mermaid {
            margin: 30px 0;
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05);
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 0;
            top: 8px;
            width: 16px;
            height: 16px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 7px;
            top: 24px;
            width: 2px;
            height: calc(100% + 14px);
            background: rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Kafka时间轮机制
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">深度技术解析</span>
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    探索分布式系统中高效定时任务调度的核心技术
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <i class="fas fa-clock text-4xl mb-2"></i>
                        <p class="text-sm">高效调度</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-tachometer-alt text-4xl mb-2"></i>
                        <p class="text-sm">高性能</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-network-wired text-4xl mb-2"></i>
                        <p class="text-sm">分布式</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 md:p-12 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>分布式系统中，尤其是在高吞吐量的消息队列系统如Kafka中，处理定时任务和延迟消息是一个关键的挑战。时间轮作为一种高效的定时任务调度机制，能够在这些复杂的环境中提供卓越的性能和灵活性。
            </p>
            <p class="text-lg leading-relaxed text-gray-700 mt-6">
                Kafka不仅需要处理不断涌入的消息，还需要高效地管理各种定时任务，比如消息的延迟投递、重试策略和过期消息的清理。这些任务的调度和管理直接影响到系统的整体性能和用户体验。
            </p>
        </div>

        <!-- Time Wheel Concept Visualization -->
        <div class="content-card rounded-2xl shadow-xl p-8 md:p-12 mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">时间轮概念可视化</h2>
            <div class="mermaid">
                graph LR
                    A[时间轮] --> B[环形结构]
                    A --> C[时间槽]
                    A --> D[时间指针]
                    
                    B --> B1[固定槽位数量]
                    B --> B2[循环推进]
                    
                    C --> C1[存储定时任务]
                    C --> C2[固定时间粒度]
                    
                    D --> D1[周期性移动]
                    D --> D2[触发任务执行]
                    
                    style A fill:#667eea,stroke:#764ba2,stroke-width:3px,color:#fff
                    style B fill:#f093fb,stroke:#f5576c,stroke-width:2px,color:#fff
                    style C fill:#f093fb,stroke:#f5576c,stroke-width:2px,color:#fff
                    style D fill:#f093fb,stroke:#f5576c,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Core Features -->
        <div class="grid md:grid-cols-3 gap-8 mb-12">
            <div class="feature-card">
                <div class="icon-wrapper">
                    <i class="fas fa-rocket text-white text-2xl"></i>
                </div>
                <h3 class="text-xl font-bold text-gray-800 mb-3">O(1)时间复杂度</h3>
                <p class="text-gray-600">任务的插入、删除和查找操作都具有常数时间复杂度，无论任务数量多少，性能始终保持稳定。</p>
            </div>
            
            <div class="feature-card">
                <div class="icon-wrapper">
                    <i class="fas fa-memory text-white text-2xl"></i>
                </div>
                <h3 class="text-xl font-bold text-gray-800 mb-3">内存优化</h3>
                <p class="text-gray-600">使用固定数量的时间槽存储任务，避免了动态内存分配，有效减少内存开销。</p>
            </div>
            
            <div class="feature-card">
                <div class="icon-wrapper">
                    <i class="fas fa-layer-group text-white text-2xl"></i>
                </div>
                <h3 class="text-xl font-bold text-gray-800 mb-3">高并发支持</h3>
                <p class="text-gray-600">环形结构和分段锁机制使其能够在高并发场景下高效处理大量定时任务。</p>
            </div>
        </div>

        <!-- Working Principle -->
        <div class="content-card rounded-2xl shadow-xl p-8 md:p-12 mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">时间轮工作原理</h2>
            
            <div class="space-y-6">
                <div class="timeline-item">
                    <h3 class="text-xl font-semibold text-gray-800 mb-2">
                        <i class="fas fa-plus-circle text-purple-600 mr-2"></i>任务插入
                    </h3>
                    <p class="text-gray-600">计算任务的执行时间相对于当前时间的偏移量，将任务插入到相应的时间槽中。</p>
                </div>
                
                <div class="timeline-item">
                    <h3 class="text-xl font-semibold text-gray-800 mb-2">
                        <i class="fas fa-forward text-purple-600 mr-2"></i>时间推进
                    </h3>
                    <p class="text-gray-600">时间轮指针周期性移动，每次移动一个时间槽的距离，模拟时间流逝。</p>
                </div>
                
                <div class="timeline-item">
                    <h3 class="text-xl font-semibold text-gray-800 mb-2">
                        <i class="fas fa-play-circle text-purple-600 mr-2"></i>任务执行
                    </h3>
                    <p class="text-gray-600">当指针到达某个时间槽时，该槽中的所有任务被触发并执行。</p>
                </div>
                
                <div class="timeline-item">
                    <h3 class="text-xl font-semibold text-gray-800 mb-2">
                        <i class="fas fa-sync-alt text-purple-600 mr-2"></i>任务更新
                    </h3>
                    <p class="text-gray-600">执行完的任务被移除，需要重新调度的任务被插入到新的时间槽中。</p>
                </div>
            </div>
        </div>

        <!-- Kafka Implementation -->
        <div class="content-card rounded-2xl shadow-xl p-8 md:p-12 mb-12">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">Kafka中的时间轮实现</h2>
            
            <div class="highlight-box mb-8">
                <h3 class="text-2xl font-bold mb-4">
                    <i class="fas fa-lightbulb mr-2"></i>设计目标
                </h3>
                <ul class="space-y-2 text-lg">
                    <li><i class="fas fa-check mr-2"></i>高效的定时任务调度</li>
                    <li><i class="fas fa-check mr-2"></i>低延迟和高吞吐量</li>
                    <li><i class="fas fa-check mr-2"></i>内存和资源的优化利用</li>
                </ul>
            </div>
            
            <div class="mermaid">
                graph TB
                    A[Kafka时间轮] --> B[延迟消息处理]
                    A --> C[定时任务调度]
                    A --> D[重试机制]